<template>
  <div class="depManage">
    <div class="header">
      <h1>部门管理</h1>
      <div class="header-right">
        <i class="el-icon-s-home"></i>
        <span class="font-left">首页</span>
        <i class="el-icon-arrow-right"></i>
        <span class="font-right">部门管理</span>
      </div>
    </div>
    <div class="content">
      <div class="bar">
        <div>
          <el-button
            type="success"
            icon="el-icon-plus"
            size="mini"
            @click="addDep"
          >
            新增
          </el-button>
          <el-button type="info" size="mini" @click="resetList">
            <i class="el-icon-refresh-left"></i>
            重置</el-button
          >
        </div>
        <div class="barRight">
          <el-input
            @keyup.enter.native="search"
            clearable
            @clear="search"
            v-model="searchContent"
            placeholder="查找名称"
            class="barInput"
            size="mini"
          ></el-input>
          <el-button
            slot="append"
            icon="el-icon-search"
            size="mini"
            @click="search"
          ></el-button>
        </div>
      </div>
      <el-table
        :data="tableData"
        :header-row-style="{ height: '53px' }"
        style="width: 100%; margin-bottom: 10px padding: 0"
        :row-class-name="tableRowClassName"
        class="tableBox"
        row-key="depId"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        Lazy
      >
        <el-table-column prop="depId" label="ID" width="60" align="center">
        </el-table-column>
        <el-table-column
          prop="depName"
          label="名称"
          width="240"
        ></el-table-column>
        <el-table-column prop="depPho" label="电话" width="250">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="500">
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="50"> </el-table-column>
        <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="info"
              @click="up(scope.$index, scope.row, scope.row.depId)"
              >上移</el-button
            >
            <el-button
              size="mini"
              type="info"
              @click="down(scope.row.depId, scope.row, scope.row.depId)"
              >下移</el-button
            >
            <el-button size="mini" type="primary" @click="changeDep(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="goDelete(scope.row.depId)"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="success"
              v-if="scope.row.pid == 0"
              @click="addChildrenDep(scope.row, scope.row.depId)"
              >新增</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- <Pagination
        @getPagination="getPagination"
        :total="total"
        :pageSize="pageSize"
      ></Pagination> -->
      <el-pagination
        @current-change="getPagination"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total"
        :total="total"
        @getPagination="getPagination"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
export default {
  name: "",
  components: {
    Pagination,
  },
  mixins: [],
  props: ["msg"],
  data() {
    return {
      input: "",
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //搜索内容
      searchContent: "",
      //表格数据
      tableData: [],
    };
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getAllDepList();
  },
  methods: {
    resetList() {
      this.getAllDepList();
    },
    //获取点击的页码
    getPagination(page) {
      this.currentPage = page;
      this.getAllDepList(this.currentPage, this.pageSize);
    },
    //获取表格数据
    async getAllDepList() {
      let res = await this.$api.user.getDepList({
        currentPage: 1,
        pageSize: 100,
      });
      console.log(res);
      if (res.data.code === 200) {
        // this.tableData = res.data.data.list;
        let arr = res.data.data.list;
        let tree = this.arrayToTree(arr, 0);
        this.tableData = tree;
        console.log(this.tableData);
        //页码
        this.total = res.data.data.total;
      } else {
        console.log("请求失败");
        this.tableData = [];
        this.pageSize = 0;
        this.total = 1;
      }
    },
    //递归扁平转树形
    arrayToTree(arr, rootNode) {
      const tree = [];
      arr.forEach((item) => {
        if (item.pid == rootNode) {
          // 找到根节点之后, 就去找下面有没有对应的子节点
          const children = this.arrayToTree(arr, item.depId);
          // 如果 children 的长度大于0, 说明找到了子节点
          children.length > 0 ? (item.children = children) : 0;
          tree.push(item); // 将内容加入数组中
        }
      });
      return tree;
    },
    async up(index, row, depId) {
      this.$api.user.subSort(depId);
      this.getAllDepList();
      /* var num = this.tableData.findIndex((value) => value == row);
      if (num > 0) {
        let upData = this.tableData[num - 1];
        this.tableData.splice(num - 1, 1);
        this.tableData.splice(num, 0, upData);
      } else {
        this.$message({
          message: "已经是第一条",
          type: "warning",
        });
      } */
    },
    async down(index, row, depId) {
      let res = await this.$api.user.addSort(depId);
      this.getAllDepList();
      /* var num = this.tableData.findIndex((value) => value == row);
      if ((num + 1) == this.tableData.length) {
        this.$message({
          message: "已经是最后一条",
          type: "warning",
        });
      } else {
        let downData = this.tableData[num + 1];
        this.tableData.splice(num + 1, 1);
        this.tableData.splice(num, 0, downData);
      } */
    },
    //搜索
    async search() {
      let res = await this.$api.user.searchDep(
        this.currentPage,
        this.pageSize,
        this.searchContent.toString()
      );
      this.tableData = res.data.data.list;
      this.total = res.data.data.total;
    },
    //表格发光
    tableRowClassName({ row }) {
      if (row.pid === 0) {
        return "success-row";
      }
      return "";
    },
    //新增部门
    addDep() {
      this.$router.push("/user/addDep");
    },
    //新增子部门
    addChildrenDep(row, depId) {
      console.log(row);
      this.$router.push({
        name: "addChildrenDep",
        params: {
          row,
          depId,
        },
      });
    },
    //更改页面
    changeDep(row) {
      this.$router.push({
        name: "changeDep",
        params: {
          row,
        },
      });
    },
    //删除
    goDelete(depId) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteConfirm(depId);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async deleteConfirm(depId) {
      let res = await this.$api.user.deleteDep(depId);
      console.log(res);
      if (res.data.code === 200) {
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        //更新视图-------------------------
        this.getAllDepList(this.currentPage, this.pageSize);
      } else {
        this.$message.error("删除失败");
      }
    },

    /* setTreeData(source) {
      let cloneData = JSON.parse(JSON.stringify(source)); // 对源数据深度克隆
      return cloneData.filter((father) => {
        // 循环所有项，并添加children属性
        let branchArr = cloneData.filter(
          (child) => father.depId == child.depId
        ); // 返回每一项的子级数组
        branchArr.length > 0 ? (father.children = branchArr) : ""; //给父级添加一个children属性，并赋值
        return father.parentId == 0; //返回第一层
      });
    }, */

    /* arrayToTree(arr, rootNode) {
      const tree = [];
      arr.forEach((item) => {
        if (item.drugTypePid == rootNode) {
          // 找到根节点之后, 就去找下面有没有对应的子节点
          const children = this.arrayToTree(arr, item.id);
          // 如果 children 的长度大于0, 说明找到了子节点
          children.length > 0 ? (item.children = children) : 0;
          tree.push(item); // 将内容加入数组中
        }
      });
      return tree;
    }, */
  },
};
</script>
<style lang="less" scoped>
.depManage {
  height: 100%;
}
.header {
  position: relative;
  padding: 15px;
  display: flex;
  justify-content: space-between;
}
.content {
  background-color: #f5f5f5;
  border-left: 1px solid #f3f3f3;
  border-right: 1px solid #f3f3f3;
}
.header > h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
}
.header-right {
  font-size: 12px;
  display: flex;
  align-items: center;
}
.el-icon-arrow-right {
  padding: 0 5px;
  color: #ccc;
}
.el-icon-s-home {
  color: #444;
  display: inline-block;
  margin-right: 5px;
}
.font-left {
  color: #444;
}
.font-right {
  color: #777;
}
.bar {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  border-top: 3px solid #d2d6de;
  border-bottom: 1px solid #f4f4f4;
}
.barRight {
  display: flex;
}
.pa .el-button-success {
  background-color: red;
}
.el-table-column {
  color: #333333;
  height: 37px;
}
.tableBox {
  th {
    padding: 0 !important;
    height: 53px;
    line-height: 53px;
  }
  td {
    padding: 0 !important;
    height: 53px;
    line-height: 53px;
  }
}
</style>
